package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.OperationLog;
import com.example.exception.BusinessException;
import com.example.mapper.OperationLogMapper;
import com.example.service.OperationLogService;
import com.example.util.Result;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author HP
* @description 针对表【operation_log】的数据库操作Service实现
* @createDate 2025-08-18 19:33:10
*/
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog>
    implements OperationLogService {

    @Resource
    private OperationLogMapper operationLogMapper;

    @Override
    public boolean recordLog(Long operatorId, Long entityId, Integer entityType, String reason) {
        OperationLog operationLog = new OperationLog();
        if (operatorId == null) {
            operationLog.setOperatorId(null);
            operationLog.setOperatorType(0);
        }else {
            operationLog.setOperatorId(operatorId);
            operationLog.setOperatorType(1);
        }
        if (entityId == null) {
            throw new BusinessException("实体ID不能为空");
        }
        if (entityType == null){
            throw new BusinessException("实体类型不能为空");
        }
        if(reason == null || reason.isEmpty()){
            throw new BusinessException("操作描述不能为空");
        }
        operationLog.setEntityId(entityId);
        operationLog.setEntityType(entityType);
        operationLog.setReason(reason);
        int insert = operationLogMapper.insert(operationLog);
        if (insert <= 0) {
            throw new BusinessException("记录操作日志失败");
        }
        return true;
    }

    @Override
    public Result getLogsByOperatorId(Long operatorId) {
        if (operatorId == null) {
            //获取系统操作日志
            QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("operator_id", operatorId);
            queryWrapper.eq("operator_type", 0);
            queryWrapper.orderByDesc("operation_time");
            List<OperationLog> operationLogs = operationLogMapper.selectList(queryWrapper);
            return Result.success(Map.of("logs", operationLogs));
        }
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operator_id", operatorId);
        queryWrapper.orderByDesc("operation_time");
        List<OperationLog> logs = operationLogMapper.selectList(queryWrapper);
        return Result.success(Map.of("logs", logs));
    }

    @Override
    public Result getLogsByTimeRange(Date startTime, Date endTime) {
        if (startTime == null || endTime == null) {
            throw new BusinessException("时间范围不能为空");
        }
        if (startTime.after(endTime)) {
            throw new BusinessException("开始时间不能晚于结束时间");
        }

        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("operation_time", startTime, endTime);
        queryWrapper.orderByDesc("operation_time");
        List<OperationLog> logs = operationLogMapper.selectList(queryWrapper);
        return Result.success(Map.of("logs", logs));
    }

    @Override
    public Result getLogsByEntityType(Integer entityType) {
        if (entityType == null) {
            throw new BusinessException("实体类型不能为空");
        }

        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("entity_type", entityType);
        queryWrapper.orderByDesc("operation_time");
        List<OperationLog> logs = operationLogMapper.selectList(queryWrapper);
        return Result.success(Map.of("logs", logs));
    }



    @Override
    public Result listAllLogs() {
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("operation_time");
        List<OperationLog> logs = operationLogMapper.selectList(queryWrapper);
        return Result.success(Map.of("logs", logs));
    }
}




